<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: SiteController.php
// +----------------------------------------------------------------------
// | Author: gxs
// +----------------------------------------------------------------------
// | Description: 后台系统登录,退出
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History:
// +----------------------------------------------------------------------
// | 1.Date: 2018/12/28 Author: gxs Modification: 创建
// +----------------------------------------------------------------------

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Backend\SiteRequest;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Tymon\JWTAuth\Http\Parser\KeyTrait;

class SiteController extends Controller
{
    use KeyTrait;

    /**
     * SiteController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth:backend')->except(['login', 'logout']);
    }

    /**
     * login
     *
     * @param SiteRequest $request
     * @return mixed
     */
    public function login(SiteRequest $request)
    {
        $params = $request->only(['account', 'password']);
        $ttl = config('backend.jwt.expiration');
        if (!$token = auth('backend')->setTTL($ttl)->attempt($params))
            return failed('登录失败');
        $admin = auth('backend')->user();
        $admin->login_time = Carbon::now();
        $admin->save();
        return respond([], '登录成功')->cookie('token', $token, $ttl);
    }

    /**
     * 退出
     *
     * @return mixed
     */
    public function logout()
    {
        auth('backend')->logout();
        return respond([], '登出成功')->cookie('token', null);
    }


}